

#include "user_struct.h"
#include "IO_check_handle.h"
#include "uart_comm_ex_api.h"



STR_IO_CHECK_HDL chargeCheck;
STR_IO_CHECK_HDL limitX1Check;
STR_IO_CHECK_HDL limitX2Check;


/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/

void key_check_init(void)
{
	
	// IO input signal
	
	chargeCheck.check_cnt = 0;
	chargeCheck.hold_cnt  = 0;
	chargeCheck.check_holdtime = CHARGE_CHECK_HOLD_TIME;
	chargeCheck.clear_holdtime = CHARGE_CLEAR_HOLD_TIME;
	
	limitX1Check.check_cnt = 0;
	limitX1Check.hold_cnt  = 0;
	limitX1Check.check_holdtime = LIMIT_CHECK_HOLD_TIME;
	limitX1Check.clear_holdtime = LIMIT_CLEAR_HOLD_TIME;
	
	limitX2Check.check_cnt = 0;
	limitX2Check.hold_cnt  = 0;
	limitX2Check.check_holdtime = LIMIT_CHECK_HOLD_TIME;
	limitX2Check.clear_holdtime = LIMIT_CLEAR_HOLD_TIME;

}





/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/

uint8_t IO_check_handle(STR_IO_CHECK_HDL *hdl, uint8_t input)
{
	if(input == 0)
	{
		hdl->check_cnt++;
		if(hdl->check_cnt > hdl->check_holdtime)
		{
			hdl->check_cnt = hdl->check_holdtime;
			hdl->hold_cnt = 0;
			hdl->input_rslt = 1;
		}
	}
	else
	{
		hdl->check_cnt = 0;
		hdl->hold_cnt++;
		
		if(hdl->hold_cnt > hdl->clear_holdtime)
		{
			hdl->hold_cnt = 0;
			hdl->input_rslt = 0;
		}
	}
	
	hdl->input_real = input;
	
	return hdl->input_rslt;
}




/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/

void key_status_check(void)
{
	
	userHdl.signal.limit_X1 = !IO_check_handle(&limitX1Check, get_gpio_input(&userGPIO[IN_ELE_POS_X1]));
	userHdl.signal.limit_X2 = !IO_check_handle(&limitX2Check, get_gpio_input(&userGPIO[IN_ELE_POS_X2]));
	
	userHdl.signal.charge_status = IO_check_handle(&chargeCheck, get_gpio_input(&userGPIO[IN_CHARGE_STATUS]));
	
}







